﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows;

namespace TELOS_Konverter
{
    class FileHandler
    {
        private MessageHandler mh = new MessageHandler();
        private StringHandler sth = new StringHandler();
        private HtmlHandler hh = new HtmlHandler();
        private AsxHandler ah = new AsxHandler();
        private SmiHandler smh = new SmiHandler();
        private FileOperations fo = new FileOperations();
        private ErrorHandler eh = new ErrorHandler();

        private const int TYPE_VIDEO = 1;
        private const int TYPE_COPYRIGHT = 2;

        public int getHTMLTypeForSearchVideo() { return TYPE_VIDEO; }
        public int getHTMLTypeForSearchCopyright() { return TYPE_COPYRIGHT; }

        /// <summary>
        /// This function will rewrite a complete file structure
        /// </summary>
        /// <param name="htmlFilePath">Path to the html file that hints at the rest of the structure</param>
        /// <returns>True if rewriting was succussful, false otherwise</returns>
        public bool rewriteFile(String htmlFilePath, FileInformation fi)
        {            
            bool success = true;
            if (fo.assignFileNames(htmlFilePath, fi))
            {
                success = hh.rewriteHTMLContent(htmlFilePath);
                if (success)
                    success = ah.writeASXFile();
                if (success)
                    success = smh.writeSMIFile();
                //setNewHTMLFileContent(readFile(htmlFilePath));
                if (!success)
                {
                    eh.createError("The following file created an error: " + htmlFilePath);
                }
                return success;
            }
            else // in this case, there was nothing to be changed
                return true;
        }

        public List<string> DirSearch(string sDir, int type)
        {
            List<string> cache = new List<string> { };
            try
            {
                // root directory
                foreach (string f in Directory.GetFiles(sDir))
                {
                    if ((sth.isStringInString(f, ".html", true)) && (((type == TYPE_VIDEO) && (containsVideo(f, true)))
                                                             || (type == TYPE_COPYRIGHT)))
                    {
                        cache.Add(f);
                        
                    }
                    
                }

                // subdirectory
                foreach (string d in Directory.GetDirectories(sDir))
                {
                    foreach (string f in Directory.GetFiles(d))
                    {
                        if ((sth.isStringInString(f, ".html", true)) && (((type == TYPE_VIDEO) && (containsVideo(f, true)))
                                                             || (type == TYPE_COPYRIGHT)))
                        {
                            cache.Add(f);
                            
                        }
                    }
                    DirSearch(d, type);
                }
            }
            catch (Exception e)
            {
                eh.createError(e, "dirSearch, sDir: "+sDir);
            }

            return cache;
        }

        /// <summary>
        /// Check if a html file contains a media player.
        /// The result depends upon if the mediatype is 1. If not (could be 2), this will return false.
        /// </summary>
        /// <param name="htmlFilePath">Path to the html file</param>
        /// <returns>True if website contains media player, false otherwise</returns>
        private bool containsVideo(string htmlFileName)
        {
            bool contains = false;
            List<string> content = new List<string>(fo.readFile(htmlFileName));
            
            for (int i = 0; i < content.Count; i++)
            {
                if ((sth.isStringInString(content[i], "var mediatype=\"1\"")) || (sth.isStringInString(content[i], "<embed src=\"")))
                {
                    contains = true;
                    break;
                }
            }

            return contains;
        }        

        private bool containsVideo(string htmlFileName, bool lowercase)
        {
            bool contains = false;
            List<string> content = new List<string>(fo.readFile(htmlFileName));

            for (int i = 0; i < content.Count; i++)
            {
                // isStringInString should also be putting chars to lowercase, but just to make sure ...
                if ((sth.isStringInString(content[i].ToLower(), "var mediatype=\"1\"".ToLower(), true )) || (sth.isStringInString(content[i].ToLower(), "<embed src=\"".ToLower(), true)))
                {
                    contains = true;
                    break;
                }
            }

            return contains;
        }        
    }
}
